#!/usr/bin/make -f

# Vendor and version
version := $(shell dpkg-parsechangelog -SVersion).$(shell dpkg-vendor --query Vendor)
CXXFLAGS += -DPACKAGEVERSION='"$(version)"'

# Backends
backends := bind ldap pipe gmysql gpgsql gsqlite3 geoip lua lmdb mydns remote

DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Use new build system
%:
	dh $@ --with autoreconf --parallel

override_dh_auto_configure:
	test -f pdns/dnslabeltext.cc && mv pdns/dnslabeltext.cc debian/dnslabeltext.cc.moved || true
	./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--sysconfdir=/etc/powerdns \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--libdir='$${prefix}/lib/$(DEB_HOST_MULTIARCH)' \
		--libexecdir='$${prefix}/lib' \
		--with-dynmodules="$(backends)" \
		--with-modules="" \
		--with-system-polarssl \
		--with-pgsql-includes=`pg_config --includedir` \
		--enable-cryptopp \
		--enable-botan1.10 \
		--enable-tools \
		--enable-unit-tests

# pdns-server has a debug package
override_dh_strip:
	dh_strip --dbg-package=pdns-server-dbg

# Additional permissions
override_dh_fixperms:
	dh_fixperms
	chmod 755 debian/pdns-server/etc/resolvconf/update.d/pdns

override_dh_auto_install:
	./pdns/pdns_server --no-config --config | sed \
	  -e 's!# config-dir=.*!config-dir=/etc/powerdns!' \
	  -e 's!# daemon=.*!daemon=yes!' \
	  -e 's!# guardian=.*!guardian=yes!' \
	  -e 's!# include-dir=.*!&\ninclude-dir=/etc/powerdns/pdns.d!' \
	  -e 's!# launch=.*!&\nlaunch=!' \
	  -e 's!# setgid=.*!setgid=pdns!' \
	  -e 's!# setuid=.*!setuid=pdns!' \
	  > debian/pdns-server/etc/powerdns/pdns.conf
	dh_auto_install
	rm -f debian/pdns-server/etc/powerdns/pdns.conf-dist

# init script needs to be named pdns, not pdns-server
override_dh_installinit:
	dh_systemd_enable --name=pdns
	dh_installinit --name=pdns
	dh_systemd_start --restart-after-upgrade

# Verbose build (shows used compiler/linker and their flags)
override_dh_auto_build-arch:
	dh_auto_build -- V=1

# Verbose tests (shows used compiler/linker and their flags)
override_dh_auto_test:
	$(MAKE) -C pdns check
	#dh_auto_test -- V=1

# restore moved files
override_dh_clean:
	test -f debian/dnslabeltext.cc.moved && mv debian/dnslabeltext.cc.moved pdns/dnslabeltext.cc || true
	dh_clean
